﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using JigLibX.Collision;
using JigLibX.Physics;
using JigLibX.Geometry;
using JigLibX.Math;
using Microsoft.Xna.Framework.Graphics;
using JigLibX.Utils;
using System.Xml;
using PloobsEngine.Modelo;
#endregion

namespace PloobsEngine.Physics.XJigLib
{
    public class HeightMapObject : JigLibXObject
    {
        
        public HeightMapObject(IModelo modelo, Vector2 shift)
        {
            Body = new Body(); // just a dummy. The PhysicObject uses its position to get the draw pos
            CollisionSkin = new CollisionSkin(null);

            HeightMapInfo heightMapInfo = modelo.getModelTag() as HeightMapInfo;
            Array2D field = new Array2D(heightMapInfo.heights.GetLength(0), heightMapInfo.heights.GetLength(1));

            for (int x = 0; x < heightMapInfo.heights.GetLength(0); x++)
            {
                for (int z = 0; z < heightMapInfo.heights.GetLength(1); z++)
                {
                    field.SetAt(x, z, heightMapInfo.heights[x, z]);
                }
            }

            // move the body. The body (because its not connected to the collision
            // skin) is just a dummy. But the base class shoudl know where to
            // draw the model.
            Body.MoveTo(new Vector3(shift.X, 0, shift.Y), Matrix.Identity);

            CollisionSkin.AddPrimitive(new Heightmap(field, shift.X, shift.Y, heightMapInfo.terrainScale, heightMapInfo.terrainScale), (int) MaterialTable.MaterialID.NormalRough);

            PhysicsSystem.CurrentPhysicsSystem.CollisionSystem.AddCollisionSkin(CollisionSkin);
            
        }
        public override void DeSerialize(XmlNode node, EngineStuff engine, IModelo model, Vector3 position, Matrix rotation)
        {
            throw new NotImplementedException();
        }
        public override void Serialize(System.Xml.XmlTextWriter writer)
        {
            throw new NotImplementedException();
        }
        public override PhysicObjectTypes PhysicObjectTypes
        {
            get { return PhysicObjectTypes.HEIGHMAPOBJECT; }
        }
    }
}
